using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml.Serialization;
using SynoManager.Rss;

namespace SynoManager.DownloadStation.Subscriptions
{
    /// <summary>
    /// Class that represents a subscription to an RSS Feed
    /// </summary>
    [Serializable()]
    public class Subscription
    {
        private string title;
        private string feedUri;
        private bool autoUpdate;
        private TimeSpan updateInterval;

        private LinkDiscovery linkDiscovery;
        private string customLinkDiscoveryRegex;

        private DateTime lastCheck = DateTime.Now;

        #region Settings
        /// <summary>
        /// Gets or sets the (display) title for this subscription.
        /// </summary>
        public string Title
        {
            get { return title; }
            set { title = value;}
        }

        /// <summary>
        /// Gets or sets the URI for this feed.
        /// </summary>
        public string FeedUri
        {
            get { return feedUri; }
            set { feedUri = value; }
        }

        /// <summary>
        /// Gets or sets wether this feed should be automatically checked for updates.
        /// </summary>
        public bool AutoUpdate
        {
            get { return autoUpdate; }
            set { autoUpdate = value; }
        }

        /// <summary>
        /// Gets or sets the interval at which this feed should be checked if <see cref="AutoUpdate" /> is true.
        /// </summary>
        public TimeSpan UpdateInterval
        {
            get { return updateInterval; }
            set { updateInterval = value; }
        }

        /// <summary>
        /// Gets or sets the <see cref="SynoManager.DownloadStation.Subscriptions.LinkDiscovery" /> mode for this feed.
        /// </summary>
        public LinkDiscovery LinkDiscovery
        {
            get { return linkDiscovery; }
            set { linkDiscovery = value; }
        }

        /// <summary>
        /// Gets or sets the custom regular expression that is used when <see cref="LinkDiscovery" /> is set to <see cref="Rss.LinkDiscovery.Regex"/>
        /// </summary>
        public string CustomLinkDiscoveryRegex
        {
            get { return customLinkDiscoveryRegex; }
            set { customLinkDiscoveryRegex = value; }
        }

        /// <summary>
        /// Gets the time at which this feed was last checked.
        /// </summary>
        public DateTime LastCheck
        {
            get { return lastCheck; }
            set { lastCheck = value; }
        }
        #endregion

        #region Items
        private List<SubscriptionItem> items;
        [XmlIgnore()]
        public SubscriptionItem[] Items
        {
            get
            {
                if (items == null)
                    Update();

                return items.ToArray();
            }
        }

        /// <summary>
        /// Updates the list of items.
        /// </summary>
        public void Update()
        {
            if (items == null)
                items = new List<SubscriptionItem>();

            RssFeed feed = RssFeed.Read(feedUri.ToString());

            Debug.Assert(feed.Channels.Count == 1);
            RssChannel channel = feed.Channels[0];

            List<string> guids = new List<string>();
            foreach(SubscriptionItem item in items)
                guids.Add(item.Guid);

            foreach(RssItem item in channel.Items)
            {
                if (guids.Contains(item.Guid.Name))
                    continue;

                SubscriptionItem subscription = new SubscriptionItem(this, item);
                items.Add(subscription);
            }
        }
        #endregion
    }
}